import fileModel


def toCsv(path):
    f = fileModel.open_file(path)
    w = fileModel.open_file(path + ".csv", 'w')
    tag = 0
    for line in f:
        if tag == 0:
            w.write(','.join(['p' + str(index) for index, k in enumerate(line.split()[1:])]) + '\n')
        tag = 1
        w.write(','.join(line.split()[1:]) + "\n")

    f.close()
    w.close()


def print_data(path, tagPath, tag):
    f1 = fileModel.open_file(path)
    f2 = fileModel.open_file(tagPath)
    for line1, line2 in zip(f1, f2):
        if line2.strip() == tag:
            print(line1.strip())

    f1.close()
    f2.close()

# print_data("news_o/news.data","news_o/news.tag","health")


def convert_raw_data_to_svm(path,tag_path,Pwordmap=None):
    tag = fileModel.read_one_line_data(tag_path)
    wordmap = Pwordmap
    if Pwordmap is None:
        wordmap = fileModel.read_wordMap(path)

    with open(path,'r',encoding='utf-8') as f, open(path+'.svm','w',encoding='utf-8') as w:
        for line,oneTag in zip(f, tag):
            lines = line.split()
            pline = {}
            for word in lines:
                if word not in wordmap:
                    continue
                if wordmap[word] not in pline:
                    pline[wordmap[word]] = 0
                pline[wordmap[word]] += 1
            w.write(oneTag+" "+' '.join([str(key)+":"+str(pline[key]) for key in pline])+"\n")

    return wordmap

root ="C:/Users/ffftzh/Desktop/动机标注/动机分析/"
wordmap = convert_raw_data_to_svm(root+"train.data",root+"train.tag")
convert_raw_data_to_svm(root+"zhao.txt.seg.processed",root+"tag.txt",wordmap)
convert_raw_data_to_svm(root+"li.txt.seg.processed",root+"tag.txt",wordmap)